import Data.Char
import Control.Monad

dec :: String -> Int
dec [] = 0
dec [x] = 0
dec (x:xs) = vl + dec list 
    where c1 = ord x
          c2 = ord $ last xs
          mn = min c1 c2
          vl = (c1 + c2) - (mn * 2)
          list = init xs

main = do
    q <- readLn
    xs <- replicateM q getLine
    mapM_ (putStrLn . show .dec) xs